package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.IPanelConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther: wubang
 * @description:
 * @date: 2022-07-09 09:32
 */
@Service
public class IHomepageServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    IPanelConverter iPanelConverter;

    /**
     * @author wubang
     * @description 主页显示
     * @date 2022/7/9 10:58
     */
    @Override
    public HomePageResponse homepage() {
        HomePageResponse response = new HomePageResponse();

        try {
            //数据库查询
            List<Panel> panels = panelMapper.selectPanelContent();
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                // List<PanelContentItemDto> panelContentItemDtos = iPanelContentConverter.panelItems2PanelItemDto(panelContentItems);
                panel.setPanelContentItems(panelContentItems);
            }
            List<PanelDto> panelDtos = iPanelConverter.panelList2PanelDtoList(panels);
            //执行成功
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setPanelContentItemDtos(panelDtos);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
